package com.web22_2_1.service.impl;

import com.web22_2_1.entity.Course;
import com.web22_2_1.entity.Student;
import com.web22_2_1.mapper.CourseSQL;
import com.web22_2_1.mapper.StudentSQL;
import com.web22_2_1.service.CourseService;
import com.web22_2_1.utils.DruidDemo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CourseServiceImpl implements CourseService {
    @Override
    public List<Course> getAllCourses() {
        List<Course> list = new ArrayList<>();
        Connection connection = null;
        PreparedStatement pStatement = null;
        try {
            connection = DruidDemo.getConnection();
            pStatement = connection.prepareStatement(CourseSQL.SELECT_ALL_COURSES);
            ResultSet rs = pStatement.executeQuery();
            while (rs.next()) {
                Course course = new Course();
                course.setCourseId(rs.getInt("CourseID"));
                course.setCourseName(rs.getString("CourseName"));
                course.setCredit(rs.getFloat("Credit"));
                course.setTeacherId(rs.getInt("TeacherID"));
                list.add(course);
            }
        } catch (SQLException e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            DruidDemo.destroy(connection, pStatement, null);
        }
        return list;
    }

    @Override
    public Course getCourseById(int courseId) {
        Course course = null;
        Connection connection = null;
        PreparedStatement pStatement = null;
        try {
            connection = DruidDemo.getConnection();
            pStatement = connection.prepareStatement(CourseSQL.SELECT_COURSE_BY_ID);
            pStatement.setInt(1, courseId);
            ResultSet rs = pStatement.executeQuery();
            while (rs.next()) {
                course = new Course();
                course.setCourseId(rs.getInt("CourseID"));
                course.setCourseName(rs.getString("CourseName"));
                course.setCredit(rs.getFloat("Credit"));
                course.setTeacherId(rs.getInt("TeacherID"));

            }
        } catch (SQLException e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            DruidDemo.destroy(connection, pStatement, null);
        }
        return course;
    }

    @Override
    public Course getCourseByName(String courseName) {
        Course course = null;
        Connection connection = null;
        PreparedStatement pStatement = null;
        try {
            connection = DruidDemo.getConnection();
            pStatement = connection.prepareStatement(CourseSQL.SELECT_COURSE_BY_NAME);
            pStatement.setString(1, courseName);
            ResultSet rs = pStatement.executeQuery();
            while (rs.next()) {
                course = new Course();
                course.setCourseId(rs.getInt("CourseID"));
                course.setCourseName(rs.getString("CourseName"));
                course.setCredit(rs.getFloat("Credit"));
                course.setTeacherId(rs.getInt("TeacherID"));

            }
        } catch (SQLException e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            DruidDemo.destroy(connection, pStatement, null);
        }
        return course;
    }
}
